/**
 * Copyright (c) 2018-2022, NXOS Development Team
 * SPDX-License-Identifier: Apache-2.0
 * 
 * Contains: NX_Thread context 
 * 
 * Change Logs:
 * Date           Author            Notes
 * 2021-12-01     JasonHu           Init
 */

.text

#define __ASSEMBLY__
#include <arch/context.h>

/**
 * void NX_HalContextSwitchNext(NX_Addr nextSP);
 */
.globl NX_HalContextSwitchNext
NX_HalContextSwitchNext:
	LDR     X0, [X0]
    MOV     SP, X0
    B       HAL_ContextSwitchExit

/**
 * void NX_HalContextSwitchPrevNext(NX_Addr prevSP, NX_Addr nextSP);
 */
.globl NX_HalContextSwitchPrevNext
NX_HalContextSwitchPrevNext:
	SAVE_CONTEXT_FROM_EL1
    MOV    X2, SP
    STR    X2, [X0]            // store sp in preempted tasks TCB
    LDR    X0, [X1]            // get new task stack pointer
    MOV    SP, X0
    B      HAL_ContextSwitchExit

.global HAL_ContextSwitchExit
HAL_ContextSwitchExit:
    CLREX
    MOV     X0, SP
    RESTORE_CONTEXT